package com.forum.Controller;


import com.common.Entity.Result;
import com.common.Utils.JwtUtil;
import com.forum.Entity.Comment;
import com.forum.Entity.Post;
import com.forum.Entity.Stats;
import com.forum.Service.CommentService;
import com.forum.Service.LikeService;
import com.forum.Service.PostService;
import com.forum.Service.SysService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private PostService postService;

    @Autowired
    private SysService sysService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private LikeService likeService;

    @GetMapping("/getPostList")
    public Result getPostList(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(required = false) String topicId,
                              @RequestParam(required = false) String title,
                              @RequestParam String sortBy) {
        PageInfo<Post> pageInfo = postService.getPostList(pageNum, pageSize, topicId, title,sortBy);
        return Result.success(pageInfo);
    }

    @GetMapping("/getPostLists")
    public Result getPostLists(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(required = false) String topicId,
                              @RequestParam(required = false) String title) {
        PageInfo<Post> pageInfo =  postService.getPostLists(pageNum, pageSize, topicId, title);
        return Result.success(pageInfo);
    }

    @GetMapping("/getPostListByTitle")
    public Result getPostListByTitle(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(required = false) String title) {
        PageInfo<Post> pageInfo =  postService.getPostListByTitle(pageNum, pageSize, title);
        return Result.success(pageInfo);
    }

    @GetMapping("/myPosts")
    public Result getPostList(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(required = false) String title,
                              @RequestParam(required = false) String topicId,
                              @RequestParam String token,
                              @RequestParam String sortBy) {
        String username=JwtUtil.extractUsername(token);
        String userId=sysService.getUserByName(username);
        PageInfo<Post> pageInfo = postService.getmyPosts(pageNum, pageSize, title,topicId,userId,sortBy);
        return Result.success(pageInfo);
    }

    @GetMapping("/getHotPosts")
    public  Result getHotPosts(){
        List<Post> list = postService.getHotPosts();
        return Result.success(list);
    }

    @GetMapping("/detail/{postId}")
    public Result getPostDetail(@PathVariable("postId") String postId) {
        Post post = postService.getPostDetail(postId);
        return Result.success(post);
    }


    @PostMapping("/addPost")
    public Result addPost(@RequestBody Post post) {
        String token = post.getToken();
        String username = JwtUtil.extractUsername(token);
        post.setCreateUser(username);
        post.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));

        //远程调用
        String userId =sysService.getUserByName(username);
        post.setUserId(userId);


        postService.addPost(post);
        return Result.success();
    }

    //更新浏览量
    @PutMapping("/addViews/{postId}")
    public Result addViews(@PathVariable("postId") String postId) {
        postService.addViews(postId);
        return Result.success();
    }

    //统计
    @GetMapping("/getForumStatistics")
    public Result getForumStatistics(){
        Stats stats=postService.getForumStatistics();
        return Result.success(stats);

    }

    //删除帖子
    @DeleteMapping("/deletePost/{postId}")
    public Result deletePost(@PathVariable("postId") String postId){


        //1.删点赞记录
        List<Comment> comment = commentService.getCommentsByPostId(postId);
        for (Comment c : comment){
            likeService.deleteCommentLike(c.getCommentId());
        }
        likeService.deletePostLike(postId);
        //2.删评论记录
        commentService.deletePostComment(postId);
        //3.删帖子记录
        postService.deletePost(postId);
        return Result.success();
    }





}
